/*
 * 此页面主要是注意form 这个变量是根据contractsView.jsp页面来的
 * */
Ext.define('App.crm.paymentRecordsList', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.paymentRecordsListPanel',
    layout: 'fit',
    autoScroll: true,
    autoHeight: true,
    initComponent: function () {
        var grid, selModel, searchFlds;
        searchFlds = new Ext.util.MixedCollection();
        searchFlds.add('beginreimdate', new dateTime({
            name: 'reimdate',
            format: 'Y-m-d H:i:s',
            fieldLabel: '实际回款日期'
        }));
        searchFlds.add('endreimdate', new dateTime({
            name: 'reimdate',
            format: 'Y-m-d H:i:s'
        }));

        var toolbar = new Ext.Toolbar({
            enableOverflow: true,
            items: [
                {
                    text: "新增",
                    iconCls: 'addIcon',
                    handler: function () {
                        var paymentRecordsForm = Ext.create('App.crm.paymentRecordsForm');
                        paymentRecordsForm.show();
                        paymentRecordsForm.setTitle('新增');
                        if(form){      //自动加载企业和合同信息
                        paymentRecordsForm.down('form').getForm().findField("customersId").setValue(form.getForm().findField("customersId").getValue());
                        paymentRecordsForm.down('form').getForm().findField("customersName").setValue( form.getForm().findField("customersName").getValue());
                        paymentRecordsForm.down('form').getForm().findField("contractsId").setValue( form.getForm().findField("id").getValue());
                        paymentRecordsForm.down('form').getForm().findField("contractsName").setValue( form.getForm().findField("name").getValue());
                        }
                    }
                },
                '-',
                {
                    text: "编辑",
                    iconCls: 'editIcon',
                    handler: function () {
                        var sm = Ext.getCmp('paymentRecordsGrid').getSelectionModel();
                        var records = sm.getSelection();
                        if (records.length <= 0) {
                            Ext.Msg.alert('错误', "未选中记录，不能修改！");
                            return;
                        }
                        var paymentRecordsForm = Ext.create('App.crm.paymentRecordsForm');
                        paymentRecordsForm.show();
                        paymentRecordsForm.setTitle('编辑');
                        var data = records[0].raw;
                        var prForm= paymentRecordsForm.down('form').getForm();
                        prForm.loadRecord(records[0]);
                        prForm.findField("customersId").setValue(data.customers.id);
                        prForm.findField("customersName").setValue(data.customers.company);
                        prForm.findField("contractsId").setValue(data.contracts.id);
                        prForm.findField("contractsName").setValue( data.contracts.name);


                        prForm.findField("period").setValue(data.period.toString());
                        prForm.findField("invoice").setValue( data.invoice.toString());
                        prForm.findField("paymentMethods").setValue(data.paymentMethods.toString());
                        prForm.findField("classification").setValue( data.classification.toString());
                        if(data.reimdate!=null){
                            prForm.findField('reimdate').setValue( new Date(data.reimdate).format('yyyy-MM-dd'));
                        }
                        if(data.planReimdate!=null){
                            prForm.findField('planReimdate').setValue( new Date(data.planReimdate).format('yyyy-MM-dd'));
                        }
                    }
                },
                '-',
                {
                    text: "删除",
                    iconCls: 'deleteIcon',
                    handler: function () {
                        Ext.getCmp('paymentRecordsGrid').deleteRecord();
                    }
                },
                "->",
                searchFlds.get('beginreimdate'),
                searchFlds.get('endreimdate'),
                ,
                {
                    text: "搜索",
                    iconCls: 'searchIcon',
                    handler: function () {
                        grid.load();
                    }
                }
            ]
        });
        grid = Ext.create('gridView', {
            remoteSort: true,
            forceFit: true,
            gridId: 'paymentRecordsGrid',
            checkboxSelectionModel: true,
            searchFlds: searchFlds,
            entityClassName: 'com.rover.crm.entity.PaymentRecords',
            queryUrl: 'paymentRecordsController/query.action',
            deleteUrl: 'paymentRecordsController/delete.action',
            recordPk: [ "id" ],
            tbar: toolbar
        });
        //如果from存在，这个变量是根据contractsView.jsp中打开的
        if(form)
        {
            grid.getStore().on('beforeload', function(store, options) {
                store.proxy.extraParams.contractsId = form.getForm().findField("id").getValue();
            }, this, true);
        }
        Ext.apply(this, {items: [grid]});
        this.callParent(arguments);
    }, destroy: function () {
        Ext.getCmp('paymentRecordsList').destroy();
        this.destroy();
    }
});